<template>
	<div id="userLayout">
		<div class="topNav">
			<img src="/src/assets/logo.png"/>
			<h1>种植生产管理智能决策系统</h1>
		</div>
		<div class="login_panle">
			<div class="login_panle_right" />
			<div class="login_panle_form">
				<div class="login_panle_form_title">
					<p class="login_panle_form_title_p">欢迎登陆</p>
				</div>
				<el-form ref="loginForm" :model="loginFormData" :rules="rules" @keyup.enter="submitForm">
					<el-form-item prop="userName">
						<el-input v-model="loginFormData.userName" clearable placeholder="请输入用户名">
							<template #suffix>
								<span class="input-icon">
									<el-icon>
										<user />
									</el-icon>
								</span>
							</template>
						</el-input>
					</el-form-item>
					<el-form-item prop="password">
						<el-input v-model="loginFormData.password" clearable type="password" placeholder="请输入密码">
							<template #suffix>
								<span class="input-icon">
									<el-icon>
										<component is="lock"/>
									</el-icon>
								</span>
							</template>
						</el-input>
					</el-form-item>
					<!-- <el-form-item prop="captchaValue">
						<div class="vPicBox">
							<el-input v-model="loginFormData.captchaValue" clearable placeholder="请输入验证码" style="width: 60%" />
							<div class="vPic">
								<img v-if="picPath" :src="picPath" alt="请输入验证码" @click="getCaptchaUrl()">
							</div>
						</div>
					</el-form-item> -->
					<el-form-item>
						<el-button class="logBtn" type="primary" size="large" style="width: 46%; margin-left: 0" @click="submitForm">登
							录</el-button>
					</el-form-item>
				</el-form>
			</div>
			
			<!-- <div class="login_panle_foot">
				<div class="copyright">
					<bootomInfo />
				</div>
			</div> -->
		</div>
		<div class="footer">
			<p>版权归：重庆探程数字科技有限公司</p>
			<p>网站: <a target="_blank" href="http://www.cqtancheng.com">www.cqtancheng.com</a>电话：023-63615762    网站备案：渝ICP备2020014689号-1</p>
		</div>
	</div>
</template>

<script>
export default {
	name: 'Login',
}
</script>

<script setup>
import {reactive,ref,onMounted} from 'vue'
import { login } from '@/api/user'
//import { getCompanyList } from '@/api/system'
import {ElMessage} from 'element-plus'
import {useRouter} from 'vue-router'
import {useUserStore} from '@/pinia/modules/user'
import {setCookie,getCookie,removeCookie} from '@/utils/cookie'
import { message } from '@/utils/element'
import bootomInfo from '@/view/layout/bottomInfo/bottomInfo.vue'
const router = useRouter()

const userStore = useUserStore()
onMounted(()=>{
	//getCompanyList({page: 1, limit: 10})
})
// 验证函数
const checkuserName = (rule, value, callback) => {
	if (value.length < 2) {
		return callback(new Error('请输入正确的用户名'))
	} else {
		callback()
	}
}
const checkPassword = (rule, value, callback) => {
	if (value.length < 3) {
		return callback(new Error('请输入正确的密码'))
	} else {
		callback()
	}
}

const loginForm = ref(null)

const loginFormData = reactive({
	userName: import.meta.env.VITE_USER_NAME,
	password: import.meta.env.VITE_PASSWORD
})

const rules = reactive({
	userName: [{
		validator: checkuserName,
		trigger: 'blur'
	}],
	password: [{
		validator: checkPassword,
		trigger: 'blur'
	}]
})


const goLogin = async () => {
	const res = await login({loginType:"boss",rememberMe:true,...loginFormData})
	if(res.success){
		//userStore.setToken(res.data.token)
		userStore.setUserInfo({...res,loginTime:new Date()})
		// userStore.setDefaultRouterObject({
		// 	defaultRouter:'userList',
		// 	defaultRouterName:'用户列表'
		// })
		// router.push({name:userStore.defaultRouterObject.defaultRouter})
		router.push('/full/farm')
	}else{
		message(res.message,3)
	}
}

const submitForm = () => {
	loginForm.value.validate(async (v) => {
		if (v) {
			goLogin()
		} else {
			message('请正确填写登录信息',3)
			loginFormData.captchaValue = ''
			getCaptchaUrl()
			return false
		}
	})
}

// 动态添加路由
const addRouter = ()=>{
	router.addRoute(
		{
			path: '/11',
			name: '11',
			hidden:true,
			component: () => import('@/view/error/index.vue'),
			meta: { title: '11' }
		}
	)
}

</script>

<style lang="scss" scoped>
@import "@/style/newLogin.scss";
</style>
